import modelAbstract from "./modelAbstract";
import {images} from "../service/image";
import util from '../util'
import _ from 'lodash'
import player from "../canvas/player"
import bullet from "../canvas/bullet"
import {directionEnum} from "../directionEnum";
export default class extends modelAbstract implements IModel{
    canvas: ICanvas = player;
    name: string = "player";
    bindEvent:boolean = false;
    image(): HTMLImageElement {
        let direction = this.name + _.upperFirst(this.direction);
        return images.get(direction as any)!
    }
    render(): void {
        super.draw();
        if(this.bindEvent === false) {
            this.bindEvent = true;
            document.addEventListener("keydown",this.changeDirection.bind(this));
            document.addEventListener("keydown",this.move.bind(this));
            document.addEventListener("keydown",(event: KeyboardEvent) => {
                if(event.code == 'Space'){
                    bullet.addPlayerBullet()
                }
            })
        }
    }
    changeDirection(event:KeyboardEvent){
        switch(event.code){
            case 'ArrowUp':
                this.direction = directionEnum.top;
                break;
            case 'ArrowRight':
                this.direction = directionEnum.right;
                break;
            case 'ArrowDown':
                this.direction = directionEnum.bottom;
                break;
            case 'ArrowLeft':
                this.direction = directionEnum.left;
                break;
        }
        this.canvas.renderModels()
    }

    move(event:KeyboardEvent){
        let x = this.x;
        let y = this.y;
        switch(event.code){
            case 'ArrowUp':
                y -= 5;
                break;
            case 'ArrowRight':
                x += 5;
                break;
            case 'ArrowDown':
                y += 5;
                break;
            case 'ArrowLeft':
                x -= 5
                break;
        }
        if(util.isCanvasTouch(x,y) || util.isModelTouch(x,y)){
            return;
        }
        this.x = x;
        this.y = y;
        this.canvas.renderModels()
    }
}